<template>
  <el-drawer
    v-model="drawerVisible"
    :destroy-on-close="true"
    size="450px"
    :title="`${drawerProps.title}${t('accessControl.roleManagement.role')}`"
  >
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item :label="t('accessControl.roleManagement.roleName')" prop="roleName">
        <el-input
          v-model="drawerProps.row!.roleName"
          :placeholder="t('accessControl.roleManagement.pleaseEnterRoleName')"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item :label="t('accessControl.roleManagement.roleCode')" prop="roleCode">
        <el-input
          v-model="drawerProps.row!.roleCode"
          :placeholder="t('accessControl.roleManagement.pleaseEnterCode')"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item :label="t('accessControl.roleManagement.status')" prop="roleStateFlag">
        <el-radio v-model="roleStateFlag" label="true">{{ t("accessControl.roleManagement.enabled") }}</el-radio>
        <el-radio v-model="roleStateFlag" label="false">{{ t("accessControl.roleManagement.disabled") }}</el-radio>
      </el-form-item>
      <el-form-item :label="t('accessControl.roleManagement.networkIp')" prop="networkIp">
        <el-input
          v-model="drawerProps.row!.networkIp"
          :placeholder="t('accessControl.roleManagement.networkIp')"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item :label="t('accessControl.roleManagement.hostIp')" prop="hostIp">
        <el-input v-model="drawerProps.row!.hostIp" :placeholder="t('accessControl.roleManagement.hostIp')" clearable></el-input>
      </el-form-item>
      <el-form-item :label="t('accessControl.roleManagement.dbIp')" prop="dbIp">
        <el-input v-model="drawerProps.row!.dbIp" :placeholder="t('accessControl.roleManagement.dbIp')" clearable></el-input>
      </el-form-item>
      <el-form-item :label="t('accessControl.roleManagement.standardAppIp')" prop="yingyong">
        <el-input
          v-model="drawerProps.row!.yingyong"
          :placeholder="t('accessControl.roleManagement.standardAppIp')"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item :label="t('accessControl.roleManagement.middlewareIp')" prop="zhongjianjian">
        <el-input
          v-model="drawerProps.row!.zhongjianjian"
          :placeholder="t('accessControl.roleManagement.middlewareIp')"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item
        :label="t('accessControl.roleManagement.wechatMenuPermission')"
        v-model="list_Wechat_Resource_Home"
        prop="list_Wechat_Resource_Home"
      >
        <el-checkbox
          v-model="list_Wechat_Resource_Home.xtjkd"
          :label="t('accessControl.roleManagement.systemHealth')"
          size="large"
        />
        <el-checkbox
          v-model="list_Wechat_Resource_Home.erpxt"
          :label="t('accessControl.roleManagement.erpSystem')"
          size="large"
        />
        <el-checkbox
          v-model="list_Wechat_Resource_Home.gisxt"
          :label="t('accessControl.roleManagement.gisSystem')"
          size="large"
        />
        <el-checkbox
          v-model="list_Wechat_Resource_Home.ysxt"
          :label="t('accessControl.roleManagement.revenueSystem')"
          size="large"
        />
        <el-checkbox
          v-model="list_Wechat_Resource_Home.wzxt"
          :label="t('accessControl.roleManagement.websiteSystem')"
          size="large"
        />
        <el-checkbox
          v-model="list_Wechat_Resource_Home.wlaqxt"
          :label="t('accessControl.roleManagement.networkSecuritySystem')"
          size="large"
        />
        <el-checkbox
          v-model="list_Wechat_Resource_Home.hjzxxt"
          :label="t('accessControl.roleManagement.callCenterSystem')"
          size="large"
        />
        <el-checkbox
          v-model="list_Wechat_Resource_Home.btsodcl"
          :label="t('accessControl.roleManagement.btsoPending')"
          size="large"
        />
        <el-checkbox
          v-model="list_Wechat_Resource_Home.yjdcl"
          :label="t('accessControl.roleManagement.emailPending')"
          size="large"
        />
        <el-checkbox
          v-model="list_Wechat_Resource_Home.wlsb"
          :label="t('accessControl.roleManagement.networkDevice')"
          size="large"
        />
        <el-checkbox v-model="list_Wechat_Resource_Home.fwq" :label="t('accessControl.roleManagement.server')" size="large" />
        <el-checkbox v-model="list_Wechat_Resource_Home.sjk" :label="t('accessControl.roleManagement.database')" size="large" />
        <el-checkbox v-model="list_Wechat_Resource_Home.zjj" :label="t('accessControl.roleManagement.middleware')" size="large" />
        <el-checkbox
          v-model="list_Wechat_Resource_Home.bzyy"
          :label="t('accessControl.roleManagement.standardApp')"
          size="large"
        />
        <el-checkbox
          v-model="list_Wechat_Resource_Home.wllljc"
          :label="t('accessControl.roleManagement.networkTrafficMonitor')"
          size="large"
        />
        <el-checkbox
          v-model="list_Wechat_Resource_Home.zntj"
          :label="t('accessControl.roleManagement.intelligentStatistics')"
          size="large"
        />
      </el-form-item>
      <el-form-item label="岗位经理" prop="yingyong">
        <el-checkbox v-model="isLeader" size="large" />
      </el-form-item>
      <el-form-item label="岗位" prop="yingyong">
        <el-tree-select
          style="width: 180px"
          v-model="gangweiSelect"
          :data="gangweiData"
          multiple
          :render-after-expand="false"
          show-checkbox
          check-strictly
          check-on-click-node
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">{{ t("common.cancel") }}</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">{{ t("common.confirm") }}</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import { genderType } from "@/utils/dict";
import { ElMessage, FormInstance } from "element-plus";
import { User, RoleManager } from "@/api/interface";
import UploadImg from "@/components/Upload/Img.vue";
import UploadImgs from "@/components/Upload/Imgs.vue";
import { existByRoleCode, existByRoleName, save, editRole } from "@/api/modules/accessController";
import { useI18n } from "vue-i18n";
import { findEavEntity } from "@/api/modules/dailyWork";
const { t } = useI18n();

const rules = reactive({
  roleName: [{ required: true, message: t("accessControl.roleManagement.pleaseEnterRoleName") }],
  roleCode: [{ required: true, message: t("accessControl.roleManagement.pleaseEnterCode") }]
});

let list_Wechat_Resource_Home = reactive({
  xtjkd: false,
  erpxt: false,
  gisxt: false,
  ysxt: false,
  wzxt: false,
  wlaqxt: false,
  hjzxxt: false,
  yjdcl: false,
  wlsb: false,
  fwq: false,
  sjk: false,
  zjj: false,
  bzyy: false,
  wllljc: false,
  zntj: false,
  btsodcl: false
});
const isLeader = ref(false);
const gangweiSelect = ref<any>([]);
const gangweiData = ref<any>([]);
// const gangweiData = ref<any>([{label:'桌面',value:'桌面'},{label:'基础',value:'基础'},{label:'网络',value:'网络'},{label:'应用',value:'应用'},{label:'数据库',value:'数据库'}]);
let roleStateFlag = ref("");

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<RoleManager.ResRoleList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  // 显示小程序菜单选择
  let str = params.row.list_Wechat_Resource_Home;
  if (str == null && str == "") {
    str = "";
  }
  if (str == undefined) {
    str = "";
  }
  list_Wechat_Resource_Home.xtjkd = str.includes("系统健康度") ? true : false;
  list_Wechat_Resource_Home.erpxt = str.includes("ERP系统") ? true : false;
  list_Wechat_Resource_Home.gisxt = str.includes("GIS系统") ? true : false;
  list_Wechat_Resource_Home.ysxt = str.includes("营收系统") ? true : false;
  list_Wechat_Resource_Home.wzxt = str.includes("网站系统") ? true : false;
  list_Wechat_Resource_Home.wlaqxt = str.includes("网络安全系统") ? true : false;
  list_Wechat_Resource_Home.hjzxxt = str.includes("呼叫中心系统") ? true : false;
  list_Wechat_Resource_Home.btsodcl = str.includes("BTSO待处理") ? true : false;
  list_Wechat_Resource_Home.yjdcl = str.includes("邮件待处理") ? true : false;
  list_Wechat_Resource_Home.wlsb = str.includes("网络设备") ? true : false;
  list_Wechat_Resource_Home.fwq = str.includes("服务器") ? true : false;
  list_Wechat_Resource_Home.sjk = str.includes("数据库") ? true : false;
  list_Wechat_Resource_Home.zjj = str.includes("中间件") ? true : false;
  list_Wechat_Resource_Home.bzyy = str.includes("标准应用") ? true : false;
  list_Wechat_Resource_Home.wllljc = str.includes("网络流量监测") ? true : false;
  list_Wechat_Resource_Home.zntj = str.includes("智能统计") ? true : false;
  let roleState = params.row.roleState;
  if (roleState == true || roleState == undefined) {
    roleStateFlag.value = "true";
  } else {
    roleStateFlag.value = "false";
  }
  isLeader.value = params.row.isLeader == 0 || params.row.isLeader == undefined ? false : true;
  console.log(params.row.isLeader);
  let gangweistrres = params.row.gangweistr == undefined ? "" : params.row.gangweistr;
  if (gangweistrres == null && gangweistrres == "") {
    gangweistrres = "";
  }

  gangweiSelect.value = [];
  gangweiData.value = [];
  // gangweiData.value=[{label:'桌面',value:'桌面'},{label:'基础',value:'基础'},{label:'网络',value:'网络'},{label:'应用',value:'应用'},{label:'数据库',value:'数据库'}]
  findEavEntity().then(newParams => {
    let res = JSON.parse(JSON.stringify(newParams));
    res.data.forEach((child: any) => {
      console.log(child);
      let resdata = JSON.parse(JSON.stringify(child));
      if (resdata.desc != "系统数据" && resdata.desc != "") {
        gangweiData.value.push({ label: resdata.eavName, value: resdata.eavName });
      }
    });

    gangweiData.value.forEach((child2: any) => {
      let reschild2 = JSON.parse(JSON.stringify(child2));
      if (gangweistrres.includes(reschild2.value.toString())) {
        gangweiSelect.value.push(reschild2.value.toString());
      }
    });
  });

  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      let param = {
        roleName: "",
        roleCode: "",
        roleState: false,
        networkIp: "",
        hostIp: "",
        dbIp: "",
        yingyong: "",
        zhongjianjian: "",
        list_Wechat_Resource_Home: "",
        companyNo: "",
        roleId: "",
        isLeader: "",
        gangweistr: ""
      };
      param.roleName = drawerProps.value.row!.roleName == undefined ? "" : drawerProps.value.row!.roleName;
      param.roleCode = drawerProps.value.row!.roleCode == undefined ? "" : "ROLE_" + drawerProps.value.row!.roleCode;
      param.roleState = roleStateFlag.value == "true" ? true : false;
      param.networkIp = drawerProps.value.row!.networkIp == undefined ? "" : drawerProps.value.row!.networkIp;
      param.hostIp = drawerProps.value.row!.hostIp == undefined ? "" : drawerProps.value.row!.hostIp;
      param.dbIp = drawerProps.value.row!.dbIp == undefined ? "" : drawerProps.value.row!.dbIp;
      param.yingyong = drawerProps.value.row!.yingyong == undefined ? "" : drawerProps.value.row!.yingyong;
      param.zhongjianjian = drawerProps.value.row!.zhongjianjian == undefined ? "" : drawerProps.value.row!.zhongjianjian;
      param.companyNo = drawerProps.value.row!.companyNo == undefined ? "" : drawerProps.value.row!.companyNo.toString();
      param.roleId = drawerProps.value.row!.roleId == undefined ? "" : drawerProps.value.row!.roleId.toString();
      param.isLeader = isLeader.value == true ? "1" : "0";
      gangweiSelect.value.forEach(item => {
        if (param.gangweistr == "") {
          param.gangweistr = item;
        } else {
          param.gangweistr = param.gangweistr + "," + item;
        }
      });
      let list_Wechat_Resource_HomeStr = "";
      list_Wechat_Resource_HomeStr +=
        list_Wechat_Resource_Home.xtjkd == true ? t("accessControl.roleManagement.systemHealth") + "," : "";
      list_Wechat_Resource_HomeStr +=
        list_Wechat_Resource_Home.erpxt == true ? t("accessControl.roleManagement.erpSystem") + "," : "";
      list_Wechat_Resource_HomeStr +=
        list_Wechat_Resource_Home.gisxt == true ? t("accessControl.roleManagement.gisSystem") + "," : "";
      list_Wechat_Resource_HomeStr +=
        list_Wechat_Resource_Home.ysxt == true ? t("accessControl.roleManagement.revenueSystem") + "," : "";
      list_Wechat_Resource_HomeStr +=
        list_Wechat_Resource_Home.wzxt == true ? t("accessControl.roleManagement.websiteSystem") + "," : "";
      list_Wechat_Resource_HomeStr +=
        list_Wechat_Resource_Home.wlaqxt == true ? t("accessControl.roleManagement.networkSecuritySystem") + "," : "";
      list_Wechat_Resource_HomeStr +=
        list_Wechat_Resource_Home.hjzxxt == true ? t("accessControl.roleManagement.callCenterSystem") + "," : "";
      list_Wechat_Resource_HomeStr +=
        list_Wechat_Resource_Home.btsodcl == true ? t("accessControl.roleManagement.btsoPending") + "," : "";
      list_Wechat_Resource_HomeStr +=
        list_Wechat_Resource_Home.yjdcl == true ? t("accessControl.roleManagement.emailPending") + "," : "";
      list_Wechat_Resource_HomeStr +=
        list_Wechat_Resource_Home.wlsb == true ? t("accessControl.roleManagement.networkDevice") + "," : "";
      list_Wechat_Resource_HomeStr += list_Wechat_Resource_Home.fwq == true ? t("accessControl.roleManagement.server") + "," : "";
      list_Wechat_Resource_HomeStr +=
        list_Wechat_Resource_Home.sjk == true ? t("accessControl.roleManagement.database") + "," : "";
      list_Wechat_Resource_HomeStr +=
        list_Wechat_Resource_Home.zjj == true ? t("accessControl.roleManagement.middleware") + "," : "";
      list_Wechat_Resource_HomeStr +=
        list_Wechat_Resource_Home.bzyy == true ? t("accessControl.roleManagement.standardApp") + "," : "";
      list_Wechat_Resource_HomeStr +=
        list_Wechat_Resource_Home.wllljc == true ? t("accessControl.roleManagement.networkTrafficMonitor") + "," : "";
      list_Wechat_Resource_HomeStr +=
        list_Wechat_Resource_Home.zntj == true ? t("accessControl.roleManagement.intelligentStatistics") + "," : "";
      param.list_Wechat_Resource_Home = list_Wechat_Resource_HomeStr;

      if (`${drawerProps.value.title}` == "新增") {
        let falg = true;
        await existByRoleName({ roleName: drawerProps.value.row!.roleName }, "roleDto").then(res => {
          if (res.toString() != "true") {
            ElMessage.error({ message: t("accessControl.roleManagement.roleNameExists") });
            falg = false;
          }
        });
        await existByRoleCode({ roleCode: "ROLE_" + drawerProps.value.row!.roleCode }, "roleDto").then(res => {
          if (res.toString() != "true") {
            ElMessage.error({ message: t("accessControl.roleManagement.codeExists") });
            falg = false;
          }
        });
        if (falg == true) {
          await save(param, "roleDto").then(res => {
            ElMessage.success({ message: t("accessControl.roleManagement.addSuccess") });
          });
          drawerProps.value.getTableList!();
          drawerVisible.value = false;
        }
      } else {
        await editRole(param, "roleDto").then(res => {
          ElMessage.success({ message: t("accessControl.roleManagement.editSuccess") });
        });
        drawerProps.value.getTableList!();
        drawerVisible.value = false;
      }
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
